//
//  UIView+HUD.m
//  DDLock
//
//  Created by ios on 2017/12/26.
//  Copyright © 2017年 saiyi. All rights reserved.
//

#import "UIView+HUD.h"

#import <MBProgressHUD/MBProgressHUD.h>

@implementation UIView (HUD)

+ (void)showTipsWithText:(NSString *)str {
    [self _setHud:kAnimationType_FADE imageName:nil text:str view:nil];
}

+ (void)show:(kAnimationType)type text:(NSString *)str {
    [self _setHud:type imageName:nil text:str view:nil];
}

+ (void)show:(kAnimationType)type text:(NSString *)str view:(UIView *)view {
    [self _setHud:type imageName:nil text:str view:view];
}

+ (void)showWithImageName:(NSString *)name text:(NSString *)str {
    [self _setHud:kAnimationType_FADE imageName:name text:str view:nil];
}
+ (void)show:(kAnimationType)type imageName:(NSString *)name text:(NSString *)str {
    [self _setHud:type imageName:name text:str view:nil];
}
+ (void)show:(kAnimationType)type imageName:(NSString *)name text:(NSString *)str view:(UIView *)view {
    [self _setHud:type imageName:name text:str view:view];
}
+ (void)showLoadingImageName:(NSString *)name text:(NSString *)str {
    [self _setLoadingHUD:kAnimationType_FADE imageName:name text:str view:kKeyWindow];
}
+ (void)showLoading:(kAnimationType)type imageName:(NSString *)name text:(NSString *)str {
    [self _setLoadingHUD:type imageName:name text:str view:kKeyWindow];
}
+ (void)showLoading:(kAnimationType)type imageName:(NSString *)name text:(NSString *)str view:(UIView *)view {
    [self _setLoadingHUD:type imageName:name text:str view:view];
}

#pragma mark - private
// 核心
+ (void)_setHud:(kAnimationType)type imageName:(NSString *)name text:(NSString *)str view:(UIView *)view {
    
    if (view == nil) {
        view = kKeyWindow;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (name == nil) {
        hud.mode = MBProgressHUDModeText;
        hud.backgroundView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.3];
    } else {
        hud.mode = MBProgressHUDModeCustomView;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
        hud.customView = imageView;
        hud.margin = 20.0f;
        hud.bezelView.backgroundColor = UIColorFromRGB(0xB0B0B0);
        hud.label.textColor = [UIColor whiteColor];
    }
    hud.label.text = str;
    
    switch (type) {
        case kAnimationType_FADE:
        {
            hud.animationType = MBProgressHUDAnimationFade;
        }
            break;
        case kAnimationType_ZOOM:
        {
            hud.animationType = MBProgressHUDAnimationZoom;
        }
        default:
            break;
    }
    [hud hideAnimated:YES afterDelay:1.0];
    
}



+ (void)_setLoadingHUD:(kAnimationType)type imageName:(NSString *)name text:(NSString *)str view:(UIView *)view {

    UIImage *image = [[UIImage imageNamed:name] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    CGSize imageSize = image.size;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CGFloat maxWH = imageSize.width > imageSize.height ? imageSize.width : imageSize.height;
    
    CGFloat layerRadius = maxWH/2.0f + 3;
    CGFloat diameter = layerRadius * 2.0f;
    
    CGFloat layerX = .0f;
    CGFloat layerY = .0f;
    if (imageSize.width > imageSize.height) {
        layerY += (imageSize.height - imageSize.width) * 0.5;
    } else if(imageSize.width < imageSize.height) {
        layerX += (imageSize.width - imageSize.height) * 0.5;
    }
    
    // 白色背景
    CAShapeLayer *whiteLayer = [CAShapeLayer layer];
    whiteLayer.fillColor = [UIColor clearColor].CGColor;
    whiteLayer.strokeColor = [UIColor clearColor].CGColor;
    whiteLayer.lineCap = kCALineCapRound;
    whiteLayer.lineJoin = kCALineJoinRound;
    
    // 调整位置
    layerX -= 1.0f;
    layerY -= 3.0f;
    
    whiteLayer.frame = CGRectMake(layerX, layerY, diameter,  diameter) ;
    
    UIBezierPath *whiteP = [UIBezierPath bezierPathWithArcCenter:CGPointMake(diameter * 0.5f, diameter  * 0.5f) radius:layerRadius + 2.0f startAngle:.0f endAngle:M_PI * 2.0f clockwise:YES];
    whiteLayer.path = whiteP.CGPath;

    whiteLayer.strokeColor = [UIColor whiteColor].CGColor;
    whiteLayer.lineWidth=2.0f;
    
    [imageView.layer addSublayer:whiteLayer];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor clearColor].CGColor;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.frame = CGRectMake(layerX, layerY,  diameter,  diameter) ;

    UIBezierPath *p = [UIBezierPath bezierPathWithArcCenter:CGPointMake(diameter * 0.5f, diameter  * 0.5f) radius:layerRadius + 2.0f startAngle:.0f endAngle:M_PI_2 * 0.3f clockwise:YES];
    shapeLayer.path = p.CGPath;
    
    shapeLayer.strokeColor = UIColorFromRGB(0xD96B50).CGColor;
    shapeLayer.lineWidth = 2.0f;

    [imageView.layer addSublayer:shapeLayer];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = @0;
    animation.toValue = @(2.f*M_PI);
    animation.duration = 1.0;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;
    [shapeLayer addAnimation:animation forKey:nil];
    
    UIView *addView = view;
    if (view == nil) {
        addView = kKeyWindow;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:addView animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    if (type == kAnimationType_FADE) {
        hud.animationType = MBProgressHUDAnimationFade;
    } else if (type == kAnimationType_ZOOM) {
        hud.animationType = MBProgressHUDAnimationZoom;
    }
    hud.customView = imageView;
    hud.label.text = str;
    hud.label.textColor = [UIColor whiteColor];
    hud.margin = 30.0f;

    hud.bezelView.backgroundColor = UIColorFromRGB(0xB0B0B0);
  
}

+ (void)hideForView:(UIView *)view {
    UIView *hiddenView = view;
    if (view == nil) {
        hiddenView = kKeyWindow;
    }
    [MBProgressHUD hideHUDForView:hiddenView animated:YES];
}



@end
